package com.besidewallpaper.wallpaper.controller;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.besidewallpaper.wallpaper.dao.WallpaperDao;
import com.besidewallpaper.wallpaper.dto.WallpaperEntityDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import com.besidewallpaper.wallpaper.entity.WallpaperEntity;
import com.besidewallpaper.wallpaper.service.WallpaperService;
import com.besidewallpaper.wallpaper.utils.PageUtils;
import com.besidewallpaper.wallpaper.utils.R;




/**
 * 
 *
 * @author sotsukisaki
 * @email 3156263699@qq.com
 * @date 2024-09-26 15:00:30
 */
@RestController
@RequestMapping("wallpaper/wallpaper")
public class WallpaperController {
    @Autowired
    private WallpaperService wallpaperService;
    @Autowired
    private WallpaperDao wallpaperDao;

    @RequestMapping("/list")
    public R list(){
        List<WallpaperEntity> entityList = wallpaperDao.selectAll();
        List<WallpaperEntityDTO> dtoList = new ArrayList<>();
        for (WallpaperEntity entity : entityList) {
            WallpaperEntityDTO dto = new WallpaperEntityDTO();
            BeanUtils.copyProperties(entity, dto);
            dtoList.add(dto);
        }
        return R.ok().put("list", dtoList);
    }

    /**
     * 倒序查询
     */
    @GetMapping("/listDesc")
    //@RequiresPermissions("member:wallpaper:list")
    public R listDesc(){
        List<WallpaperEntity> entityList = wallpaperDao.selectAllByDesc();
        List<WallpaperEntityDTO> dtoList = new ArrayList<>();
        for (WallpaperEntity entity : entityList) {
            WallpaperEntityDTO dto = new WallpaperEntityDTO();
            BeanUtils.copyProperties(entity, dto);
            dtoList.add(dto);
        }
        return R.ok().put("list", dtoList);
    }
    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("member:wallpaper:info")
    public R info(@PathVariable("id") Long id){
		WallpaperEntity wallpaper = wallpaperService.getById(id);

        return R.ok().put("wallpaper", wallpaper);
    }

    /**
     * 信息
     */
    @RequestMapping("/{type_name}")
    public R typeList(@PathVariable("type_name") String pic_type){
        List<WallpaperEntity> list = wallpaperDao.queryPicTypeList(pic_type);
        return R.ok().put("list", list);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("member:wallpaper:save")
    public R save(@RequestBody WallpaperEntity wallpaper){
		wallpaperService.save(wallpaper);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("member:wallpaper:update")
    public R update(@RequestBody WallpaperEntity wallpaper){
		wallpaperService.updateById(wallpaper);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("member:wallpaper:delete")
    public R delete(@RequestBody Long[] ids){
		wallpaperService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }


    /**
     *  轮播
     */
    @RequestMapping("/carousel")
    public R carousel(){
    ;

        List<WallpaperEntity> list=wallpaperService.query12List();

        return R.ok().put("list", list);
    }

    /**
     *   下载
     */
    @RequestMapping("/download/{id}")
    public ResponseEntity<byte[]> downloadImage(@PathVariable("id") Long id) {
        String picUrl = wallpaperDao.getByIdWithURL(id);
        try {
            URL url = new URL(picUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            InputStream inputStream = connection.getInputStream();
            byte[] buffer = new byte[4096];
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer))!= -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
            inputStream.close();
            byte[] wallpaperData = outputStream.toByteArray();
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.IMAGE_PNG);
            headers.setContentDispositionFormData("attachment", "image.png");
            return ResponseEntity.ok().headers(headers).body(wallpaperData);
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

}
